package cn.ssm.controller;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


import cn.ssm.pojo.Schedule;
import cn.ssm.service.ScheduleService;
/**
 * 
 * 排片表控制层
 * @author aihui
 *
 */
@Controller
@RequestMapping("/schedule")
public class ScheduleController {
	@Autowired
	ScheduleService scheduleService;
	
	//添加排片表信息
	@RequestMapping("/addSchedule.htm")
	@ResponseBody
	public String addSchedule(){
		Schedule schedule= new Schedule();
		schedule.setMovieId(1);
		schedule.setHallId(1);
		schedule.setPrice(12.85);
		schedule.setShowTime(new Date());
		int count= scheduleService.addSchedule(schedule);
		if(count>0){
			return "添加成功，影响的行数为:"+count;
		}else{
			return "添加失败";
		}
	}
	//删除排片表信息
	@RequestMapping("/delSchedule.htm")
	@ResponseBody
	public String delSchedule(){
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("id", 9);
		//map.put("movieId", 1);
		
		int count = scheduleService.delSchedule(map);
		if(count>0){
			return "删除成功，影响的行数为："+count;
		}else{
			return "删除失败！";
		}
	}
	//修改排片表信息
	@RequestMapping("/updateSchedule.htm")
	@ResponseBody
	public String updateSchedule(){
		Map<String,Object> map=new HashMap<String, Object>();
		map.put("id", 8);
		map.put("movieId", 2);
		
		int count = scheduleService.updateSchedule(map);
		if(count>0){
			return "修改成功，影响的行数为："+count;
		}else{
			return "修改失败！";
		}
	}
	//查询排片表信息
	@RequestMapping("/getScheduleList.htm")
	@ResponseBody
	public Object getScheduleList(Integer sid,Integer mid,String showTime){
		Map<String,Object> map1 = new HashMap<String, Object>();
		List<Schedule> schedules =null;
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("sid", sid);
		map.put("mid", mid);
		map.put("showTime", showTime);
		try{
			schedules = scheduleService.getScheduleList(map);
			for (Schedule schedule : schedules) {
				//计算出结束的时间
				Date overTime=schedule.getShowTime();
				long time = overTime.getTime();
				int duration = schedule.getMovie().getDuration();
				long overTime1 = time+duration*60000; 
				DateFormat ymdhmsFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				String TimeStr= ymdhmsFormat.format(overTime1);
				schedule.setOverTime(TimeStr);
				System.out.println("测试:"+TimeStr);
			}
			
			map1.put("result", schedules);
			map1.put("message", "查询成功！");
			map1.put("success", true);
		}catch(Exception e){
			map1.put("message","查询失败！");
			map1.put("success", false);
		}
		return map1;
	}
	
	//根据排片id查询    黄少涛
	@RequestMapping("/getScheduleByscheduleId")
	@ResponseBody
	public Object getScheduleById(Integer scheduleId){
		Map<String,Object> map = new HashMap<String, Object>();
		Schedule schedule =null;
		try{
			schedule = scheduleService.getScheduleById(scheduleId);
			map.put("result", schedule);
			map.put("message", "查询成功！");
			map.put("success", true);
		}catch(Exception e){
			map.put("message","查询失败！");
			map.put("success", false);
		}
		return map;
	}
}
